import os

import MSConfig; MSConfig.init('.')
initRequired    = True
documentHead    = None
documentFoot    = None
def init():
    global initRequired
    global documentHead
    global documentFoot

    f = open(MSConfig.config.get('main', 'html') + 'head.html', 'r')
    documentHead = [f.read()]
    f = open(MSConfig.config.get('main', 'html') + 'foot.html', 'r')
    documentFoot = [f.read()]
    initRequired = False

if initRequired: init()


## PATTERN FUNCTIONS TO DISPLAY PAGE ##
def putHeader(functor, status, content, mimetype='text'):
    statuses =  {
                200: '200 OK',
                400: '400 Bad Request',
                404: '404 Not Found',
                }
    s = statuses.get(status, statuses[400])
    functor(s, [('Content-Type', mimetype + '/' + content)])

def get404(functor, name):
    putHeader(functor, 404, 'html')
    return ['<h3>page ' + name + ' not found</h3>']

def getHead(): return documentHead
def getFoot(): return documentFoot

# OBJECT PATTERNS #
def openObjectCommonTable():
    val =   '\
            <table>\
                <thead>\
                    <tr>\
                        <th>#</th>\
                        <th>name</th>\
                        <th>msId</th>\
                        <th>pcnId</th>\
                    </tr>\
                </thead>\
                <tbody>\
            '
    return [val]
def closeObjectCommonTable():
    val =   '\
                </tbody>\
            </table>\
            '
    return [val]
def getObjectCommonInfo(num, obj):
    val =   '\
                    <tr>\
                        <th>' + str(num) + '</th>\
                        <th>' + str(obj.objId) + '</th>\
                        <th>' + str(obj.storage.meta['msId']) + '</th>\
                        <th>' + str(obj.storage.meta['pcnId']) + '</th>\
                    </tr>\
            '
    return [val]
def getObjectDetailedInfo(obj):
    val =   '\
            <table>\
                <thead>\
                    <tr>\
                        <th>option</th>\
                        <th>value</th>\
                    </tr>\
                </thead>\
                <tbody>\
            '
    for k,v in obj.storage.meta.iteritems():
        val +=  '\
                    <tr>\
                        <th>' + str(k) + '</th>\
                        <th>' + str(v) + '</th>\
                    </tr>\
                '
    val     +=  '\
                    <tr>\
                        <th>txn</th>\
                        <th>' + str(obj.storage.txn) + '</th>\
                    </tr>\
                '
    val +=  '\
                </tbody>\
            </table>\
            '
    return [val]

def openObjectEventTable(Sortable=False):
    tablename = 'objectEventTable'
    if Sortable: tablename += 'Sortable'
    val =   '\
            <table class="zebra-striped" id="%s">\
                <thead>\
                    <tr>\
                        <th class="header">time</th>\
                        <th class="yellow header">id</th>\
                        <th class="blue header">pgsm type</th>\
                        <th class="green header">contactId</th>\
                    </tr>\
                </thead>\
                <tbody class="objectEventTableBody" id="objectEventTableBody">\
            ' % (tablename)
    return [val]
def closeObjectEventTable():
    val =   '\
                </tbody>\
            </table>\
            '
    return [val]
def getObjectEventCommonInfo(e):
    val =   '\
                    <tr>\
                        <th>' + str(e.eventGenerateTime) + '</th>\
                        <th>' + str(e.eventId) + '</th>\
                        <th>' + str(e.pgsmType) + '</th>\
                        <th>' + str(e.contactIdFull) + '</th>\
                    </tr>\
            '
    return [val]


## PATTERN FUNCTIONS TO HANDLE USER DATA ##
def checkName(name):
    if name and len(name) >= 4 and name.find('..') == -1: return True
    return False
def checkFile(name):
    if os.path.exists(name): return True
    return False
